import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isFunction from "lodash/isFunction";

var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;

import React from 'react';
import { Modal, Input, Form } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
import { encryptPwd } from 'utils/utils';
import { validatePasswordRule } from "../../../utils/validator";
var FormItem = Form.Item;
var Password = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Password, _React$Component);

  var _super = _createSuper(Password);

  function Password() {
    _classCallCheck(this, Password);

    return _super.apply(this, arguments);
  }

  _createClass(Password, [{
    key: "saveBtnClick",
    value: function saveBtnClick() {
      var _this$props = this.props,
          onOk = _this$props.onOk,
          form = _this$props.form,
          publicKey = _this$props.publicKey;

      if (!_isFunction(onOk)) {
        return;
      }

      form.validateFields(function (err, values) {
        if (!err) {
          // todo
          onOk({
            password: encryptPwd(values.password, publicKey),
            anotherPassword: encryptPwd(values.anotherPassword, publicKey)
          });
        }
      });
    }
    /**
     * 检查 确认密码是否与密码一致
     * @param {String} rule
     * @param {String} value
     * @param {Function} callback
     */

  }, {
    key: "validatePasswordRepeat",
    value: function validatePasswordRepeat(rule, value, callback) {
      var form = this.props.form;

      if (value && value !== form.getFieldValue('password')) {
        callback(intl.get('ciam.subAccount.view.validation.passwordSame').d('确认密码必须与密码一致'));
      } else {
        callback();
      }
    }
    /**
     * 检查 确认密码是否与密码一致
     */

  }, {
    key: "validatePasswordRepeatForPassword",
    value: function validatePasswordRepeatForPassword(e) {
      var form = this.props.form;
      var anotherPassword = form.getFieldValue('anotherPassword');
      var anotherPasswordField = {
        value: anotherPassword
      };

      if (e.target.value) {
        if (e.target.value === anotherPassword) {
          anotherPasswordField.errors = null;
        } else {
          anotherPasswordField.errors = [new Error(intl.get('ciam.subAccount.view.validation.passwordSame').d('确认密码必须与密码一致'))];
        }
      } else {
        anotherPasswordField.errors = null;
      }

      form.setFields({
        anotherPassword: anotherPasswordField
      });
    }
    /**
     * 检查 新密码与原密码不能相同
     * @param {String} rule
     * @param {String} value
     * @param {Function} callback
     */

  }, {
    key: "validateNewPasswordNotSame",
    value: function validateNewPasswordNotSame(rule, value, callback) {
      var form = this.props.form;

      if (value && value === form.getFieldValue('originalPassword')) {
        callback(intl.get('ciam.subAccount.view.validation.passwordNoSame').d('新密码不能与原密码相同'));
      } else {
        callback();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          form = _this$props2.form,
          visible = _this$props2.visible,
          onCancel = _this$props2.onCancel,
          confirmLoading = _this$props2.confirmLoading,
          loginName = _this$props2.editRecord.loginName,
          _this$props2$password = _this$props2.passwordTipMsg,
          passwordTipMsg = _this$props2$password === void 0 ? {} : _this$props2$password;
      var getFieldDecorator = form.getFieldDecorator;
      var labelCol = {
        md: 6
      };
      var wrapperCol = {
        md: 12
      };
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        confirmLoading: confirmLoading,
        width: 500,
        visible: visible,
        onOk: this.saveBtnClick,
        onCancel: onCancel,
        title: intl.get('ciam.subAccount.view.option.passwordUpdate').d('修改密码')
      }, /*#__PURE__*/React.createElement(FormItem, {
        required: true,
        labelCol: labelCol,
        wrapperCol: wrapperCol,
        label: intl.get('ciam.subAccount.model.user.password').d('密码')
      }, getFieldDecorator('password', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('ciam.subAccount.model.subAccount.password').d('密码')
          })
        }, {
          validator: function validator(_, value, callback) {
            validatePasswordRule(value, callback, _objectSpread(_objectSpread({}, passwordTipMsg), {}, {
              loginName: loginName
            }));
          }
        }, {
          validator: this.validateNewPasswordNotSame
        }, {
          max: 110,
          message: intl.get('cpaas.common.validation.max', {
            max: 110
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        type: "password",
        autoComplete: "new-password",
        onChange: this.validatePasswordRepeatForPassword
      }))), /*#__PURE__*/React.createElement(FormItem, {
        required: true,
        labelCol: labelCol,
        wrapperCol: wrapperCol,
        label: intl.get('ciam.subAccount.model.user.anotherPassword').d('确认密码')
      }, getFieldDecorator('anotherPassword', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('ciam.subAccount.model.subAccount.anotherPassword').d('确认密码')
          })
        }, {
          max: 110,
          message: intl.get('cpaas.common.validation.max', {
            max: 110
          })
        }, {
          validator: this.validatePasswordRepeat
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        type: "password",
        autoComplete: "new-password"
      }))));
    }
  }]);

  return Password;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "saveBtnClick", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "saveBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "validatePasswordRepeat", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "validatePasswordRepeat"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "validatePasswordRepeatForPassword", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "validatePasswordRepeatForPassword"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "validateNewPasswordNotSame", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "validateNewPasswordNotSame"), _class2.prototype)), _class2)) || _class);
export { Password as default };